package com.qef.com.mmovie.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.qef.com.mmovie.R;

/**
 * Created by guangjiego on 2017/5/15.
 */

public class PopCreator {
    private int layoutId;
    private Context mContext;
    private Animation entryAnimation, exitAnimation;
    private PopupWindow pop;
    private View popView;
    private PopCustomkListener popOnCustomListener;

    public PopCreator(int laytoutId, Context context) {
        this.layoutId = laytoutId;
        this.mContext = context;
        initPopWindow();
    }

    public PopCreator ShowLocation(View view, int align, int left, int right) {
        pop.showAtLocation(view, align, left, right);
        popView.startAnimation(entryAnimation);
        return this;
    }

    public PopCreator ShowAsDropDown(View view, int align, int left, int right) {
        pop.showAsDropDown(view, align, left, right);
        popView.startAnimation(entryAnimation);
        return this;
    }

    public PopCreator initPopWindow() {
        /**
         * 初始化底部弹出popwindow
         */
        // 加载动画
        entryAnimation = AnimationUtils.loadAnimation(mContext, R.anim.share_enter);
        exitAnimation = AnimationUtils.loadAnimation(mContext, R.anim.share_exit);
        exitAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                pop.dismiss();
            }
        });
//        ((Activity) mContext).getLayoutInflater().inflate(mlayoutid, null);
        popView = View.inflate(mContext, layoutId, null);
        pop = new PopupWindow(mContext);
        pop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        pop.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setContentView(popView);
        popView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 取消
                popView.startAnimation(exitAnimation);
                return false;
            }
        });
        return this;
    }

    /**
     * 返回对应popwindow的view 由用户对view进行点击处理相关操作
     */
    public interface PopCustomkListener {
        /**
         * 返回对应popwindow的view 由用户对view进行点击处理相关操作
         *
         * @param view
         */
        void onCustom(View view, PopupWindow popupWindow);
    }

    /**
     * 获取pop自定义pop相关内容，及点击触摸事件
     *
     * @param listener
     * @return
     */
    public PopCreator setOnCustomListener(PopCustomkListener listener) {
        popOnCustomListener = listener;
        popOnCustomListener.onCustom(popView, pop);
        return this;
    }
}
